<template>
  <div class="container">
    <div>
      <el-form ref="form" :model="form" :rules="rulesForm">
        <el-form-item label="文章标题" prop="articleTitle">
          <el-input
            v-model="form.articleTitle"
            autocomplete="off"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章简介" prop="articleExplain">
          <el-input
            type="textarea"
            v-model="form.articleExplain"
            autocomplete="off"
            placeholder="请输入文章简介"
            style="width: 400px"
          ></el-input>
        </el-form-item>

        <el-form-item label="文章作者" prop="articleAuthor">
          <el-input
            v-model="form.articleAuthor"
            autocomplete="off"
            placeholder="请输入文章作者"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章类型" prop="articleTypeNum">
          <el-select v-model="form.articleTypeNum" placeholder="请选择文章类型">
            <el-option
              v-for="item in allType"
              :key="item.id"
              :value="item.id"
              :label="item.articleType"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文章内容" prop="mdContent">
          <!-- markdown -->
          <!-- <br /> -->
          <div>
            <mavon-editor
              :ishljs="true"
              ref="md"
              @imgAdd="imgAdd"
              @imgDel="imgDel"
              style="height: 100vh"
              v-model="form.mdContent"
              @change="change"
            ></mavon-editor>
          </div>
        </el-form-item>
        <el-form-item style="text-align:right">
          <el-button type="primary" @click="confirm('form')">确 定</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getEnbalType,
  deletePicture,
  addPicture,
  addArticle,
} from "../api/article";
import common from "../utils/common";

export default {
  mounted() {
    this.getTypeList();
    console.log(this.allType);
  },
  data() {
    return {
      localhostPath:common.localhostPath,
      form: {
        articleTitle: "",
        articleExplain: "",
        articleAuthor: "",
        articleTypeNum: "",
        articleContent: "", // 转成的html
        mdContent: "", // 输入的markdown
        delivery: false,
        adminName:""
      },
      picForm: {
        picPath: "",
      },
      rulesForm: {
        articleTitle: [{ required: true, message: " ", trigger: "blur" }],
        articleExplain: [{ required: true, message: " ", trigger: "blur" }],
        articleAuthor: [{ required: true, message: " ", trigger: "blur" }],
        articleTypeNum: [{ required: true, message: " ", trigger: "blur" }],
        mdContent: [{ required: true, message: " ", trigger: "blur" }],
      },
      allType: [],
      img_file: [],
    };
  },
  methods: {
    //获取所有文章类型
    getTypeList() {
      getEnbalType().then(({ data }) => {
        data.data.forEach((item) => {
          this.allType.push(item);
        });
      });
    },
    //确定
    confirm(formName) {
      console.log(this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("success submit!!");
          console.log(this.form);
                    this.form.adminName = localStorage.getItem("realname");
          addArticle(this.form).then(({ data }) => {
            this.$message.success("添加文章成功，请为文章添加封面");
            localStorage.setItem("imgId", data.data);
            this.$router.push("/ArticleManagement/Article/ArticlePicture");
          });
        } else {
          console.log("error submit!!");
          this.$message.error("请将表单填写完整");
          return false;
        }
      }); 
    },
    change(value, render) {
      //实时获取转成html的数据
      this.form.articleContent = render;
      this.form.mdContent = value;
    },
    imgDel(pos) {
      delete this.img_file[pos];
      let str = pos[0];
      this.picForm.picPath = "/" + str.split("/")[3] + "/" + str.split("/")[4];
      deletePicture(this.picForm).then((data) => {
        console.log(data);
      });
    },
    // 将图片上传到服务器，返回地址替换到md中
    // 绑定@imgAdd event
    imgAdd(pos, $file) {
      // 第一步.将图片上传到服务器.
      var formdata = new FormData();
      formdata.append("images", $file);
      addPicture(formdata).then((data) => {
        // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
        // $vm.$img2Url 详情见本页末尾
        var url = this.localhostPath + data.data.data;
        this.$refs.md.$img2Url(pos, url);
      });
    },
  },
};
</script> 

<style scoped>
.clearfix {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 35px;
}
.el-form-item__content {
  display: flex;
  justify-content: flex-end;
}
.dialog-footer {
  float: right;
}
.el-input {
  width: 400px;
}
.el-form {
  margin-top: 30px;
}
</style>